﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manageweb_Member_ManageMember : System.Web.UI.Page
{
    protected bool IsAllow;



    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["MasterID"] == null)
        {
            Response.Redirect("~/manageweb/default.aspx");
        }

 CheckAuthority();

        if (!Page.IsPostBack)
        {
           
            BindUsers();
        }
        
       
    }

    protected void BindUsers()
    {
        SQLExpressDAL.UsersTableAdapters.UsersTableAdapter adp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();
        this.GV_UserList.DataSource = adp.GetAllUsers();
        this.GV_UserList.DataBind();
    }

    protected void Btn_Submit_Click(object sender, EventArgs e)
    {
        String msg = String.Empty;

        try
        {
            //int userIID = Convert.ToInt32(this.txtUserAccount.Text.Trim());
            //Decimal userVCurrency = Convert.ToDecimal(this.txtVCurrency.Text.Trim());

            //SQLExpressDAL.UsersTableAdapters.UsersTableAdapter adp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();
            //int flag = adp.UpdateVCurrency(userVCurrency, userIID);
            string userAccount = this.txtUserAccount.Text.Trim();
            Decimal userVCurrency = Convert.ToDecimal(this.txtVCurrency.Text.Trim());

            SQLExpressDAL.TransferRecordsTableAdapters.UsersAccountTableAdapter adp = new SQLExpressDAL.TransferRecordsTableAdapters.UsersAccountTableAdapter();
            int flag = adp.UpdateAB1(userVCurrency, userAccount);

            if (flag > 0)
            {
                msg = "修改成功!";
            }
            else
            {
                msg = "修改失败!";

            }

        }
        catch (Exception ex)
        {
            msg = ex.Message.ToString();
        }
        finally
        {
            this.LB_ManageUser.Text = msg;

            this.TABLE_Add_Mod.Visible = false;
            ClearTxt();
            BindUsers();
        }


    }

    protected void CheckAuthority()
    {
        IsAllow = Convert.ToInt32(Session["UserRole"]) != 3 ? true : false;


    }


    protected String GetMemState(String memState)
    {
        String isCancel = memState.Equals("1") ? "已作废" : "启用";


        return isCancel;

    }

    protected void GV_UserList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GV_UserList.PageIndex = e.NewPageIndex;
        BindUsers();

        ClearTxt();
        this.TABLE_Add_Mod.Visible = false;

    }

    protected void btnUpdate_Command(object sender, CommandEventArgs e)
    {
        String[] userInfos = e.CommandArgument.ToString().Split('|');

        this.txtUserAccount.Text = userInfos[0];
        this.txtUserName.Text = userInfos[1];
        this.txtVCurrency.Text = userInfos[2];

        this.TABLE_Add_Mod.Visible = true;
        this.Tab_UserScoreSet.Visible = false;
        this.Tab_UserInfoUpdate.Visible = false;


    }

    protected void ClearTxt()
    {
        this.txtUserAccount.Text = "";
        this.txtUserName.Text = "";
        this.txtVCurrency.Text = "";
    }

    protected void btnUpdateUserInfo_Command(object sender, CommandEventArgs e)
    {
        String userID = e.CommandArgument.ToString();

        SQLExpressDAL.UsersTableAdapters.UsersTableAdapter userAdp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();
        SQLExpressDAL.Users.UsersDataTable userTable = userAdp.GetSingleUserInfo(userID);


        this.TB_Address.Text = userTable.Rows[0]["UserAddress"].ToString();
        this.TB_CardCode.Text = userTable.Rows[0]["UserCardCode"].ToString();
        this.TB_City.Text = userTable.Rows[0]["UserCity"].ToString();
        this.TB_Country.Text = userTable.Rows[0]["UserCountry"].ToString();
        this.TB_Email.Text = userTable.Rows[0]["UserEmail"].ToString();
        this.TB_LoginID.Text = userTable.Rows[0]["UserID"].ToString();
        this.TB_LoginName.Text = userTable.Rows[0]["UserName"].ToString();
        this.TB_Mobile.Text = userTable.Rows[0]["UserMobile"].ToString();
        this.TB_Tel.Text = userTable.Rows[0]["UserPhone"].ToString();
        this.TB_PostCode.Text = userTable.Rows[0]["UserPostCode"].ToString();
        this.TB_City.Text = userTable.Rows[0]["UserCity"].ToString();
        this.DL_Sex.SelectedValue = userTable.Rows[0]["UserSex"].ToString() == "" ? "1" : userTable.Rows[0]["UserSex"].ToString();
        this.chkCancelAccount.Checked = userTable.Rows[0]["cdefine2"].ToString().Equals("1") ? true : false;
        this.txtRealName.Text = userTable.Rows[0]["Cdefine6"].ToString();

        this.Tab_UserInfoUpdate.Visible = true;
        this.TABLE_Add_Mod.Visible = false;
        this.Tab_UserScoreSet.Visible = false;

    }


    protected void Button3_Click(object sender, EventArgs e)
    {
        SQLExpressDAL.UsersTableAdapters.UsersTableAdapter adp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();
        SQLExpressDAL.Users.UsersDataTable userTable = new SQLExpressDAL.Users.UsersDataTable();
        SQLExpressDAL.Users.UsersRow userRow = userTable.NewUsersRow();
        try
        {
            userRow.UserAddress = TB_Address.Text;
            userRow.UserCardCode = TB_CardCode.Text;
            userRow.UserCity = TB_City.Text;
            userRow.UserCountry = TB_Country.Text;
            userRow.UserEmail = TB_Email.Text;
            userRow.UserMobile = TB_Mobile.Text;
            userRow.UserName = TB_LoginName.Text;
            userRow.UserPhone = TB_Tel.Text;
            userRow.UserPostCode = TB_PostCode.Text;
            userRow.UserSex = Convert.ToByte(DL_Sex.SelectedValue);
            userRow.UserID = TB_LoginID.Text;
            userRow.Cdefine2 = this.chkCancelAccount.Checked ? "1" : "0";

            int row = adp.UpdateUserInfo(userRow.UserName, userRow.UserPhone, userRow.UserMobile, userRow.UserEmail, userRow.UserCountry,
                userRow.UserCity, userRow.UserCardCode, userRow.UserAddress, userRow.UserPostCode, userRow.UserSex, userRow.Cdefine2, this.txtRealName.Text.Trim(), userRow.UserID);

            if (row > 0)
            {
                LB_ManageUser.Text = "会员修改成功!";

            }
            else { throw new Exception("会员修改失败!"); }
        }
        catch (Exception ex)
        {

            LB_ManageUser.Text = ex.Message;
        }
        finally
        {
            this.Tab_UserInfoUpdate.Visible = false;
            BindUsers();
        }
    }

    protected void lbtnSetScore_Command(object sender, CommandEventArgs e)
    {
        String[] userInfos = e.CommandArgument.ToString().Split('|');

        this.txtScoreAccount.Text = userInfos[0];
        this.txtScoreUserName.Text = userInfos[1];
        this.txtScoreSpecial.Text = userInfos[3];
        this.txtScoreLJ.Text = userInfos[2];

        this.Tab_UserScoreSet.Visible = true;
        this.Tab_UserInfoUpdate.Visible = false;
        this.TABLE_Add_Mod.Visible = false;
    }

    protected void btnSetScore_Click(object sender, EventArgs e)
    {
        string msg = "设置成功";

        try
        {
            decimal specialScore = Convert.ToDecimal(this.txtScoreSpecial.Text.Trim());
            decimal LjScroe = Convert.ToDecimal(this.txtScoreLJ.Text.Trim());

            SQLExpressDAL.UsersTableAdapters.UsersTableAdapter adp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();
            int flag = adp.UpdateUserScore(specialScore, LjScroe, this.txtScoreAccount.Text.Trim());

            if (flag <= 0)
            {
                msg = "设置失败";
            }

        }
        catch
        {
            msg = "设置失败";
        }
        finally
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('" + msg + "')", true);
            this.Tab_UserScoreSet.Visible = false;
            ClearScoreTxt();
        }



    }

    protected void ClearScoreTxt()
    {
        this.txtScoreAccount.Text = "";
        this.txtScoreUserName.Text = "";
    }
    //计算重新显示账户金额 
    public string GetAccountInfo(Object userID)
    {
        //SQLExpressDAL.UsersTableAdapters.UsersTableAdapter userAdp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();
        //SQLExpressDAL.Users.UsersDataTable tab = userAdp.GetSingleUserInfo(userID.ToString());

        //DataRow dr = tab.Rows[0];
        string txtMoney = "";


        SQLExpressDAL.DealRecord obj = new SQLExpressDAL.DealRecord();
        Hashtable htab = obj.GetMemAccountMoney(userID.ToString());

        if (htab["IsSuccess"].ToString().Equals("1"))
        {


            txtMoney = htab["Count"].ToString();

        }

        return txtMoney;
    }
    protected void lbtnSetAB4_Command(object sender, CommandEventArgs e)
    {
        string[] userInfo = e.CommandArgument.ToString().Split('|');
        BindUserInfo(userInfo[0], userInfo[1]);
        this.tab_SetAB4.Visible = true;
    }
    /// <summary>
    /// 获取会员的AB4账户信息
    /// </summary>
    /// <param name="userID"></param>
    /// <param name="userName"></param>
    protected void BindUserInfo(string userID, string userName)
    {
        SQLExpressDAL.TransferRecordsTableAdapters.UsersAccountTableAdapter adp = new SQLExpressDAL.TransferRecordsTableAdapters.UsersAccountTableAdapter();
        SQLExpressDAL.TransferRecords.UsersAccountRow dr = adp.GetSingleAccount(userID).Rows[0] as SQLExpressDAL.TransferRecords.UsersAccountRow;

        this.txtUserAccountAB4.Text = dr.UserID;
        this.txtUserNameAB4.Text = userName;
        this.txtAB4.Text = dr.AB4.ToString();
    }


    protected void btnAB4_Click(object sender, EventArgs e)
    {
        string msg = "设置成功!";

        try
        {
            SQLExpressDAL.TransferRecordsTableAdapters.UsersAccountTableAdapter adp = new SQLExpressDAL.TransferRecordsTableAdapters.UsersAccountTableAdapter();
            int flag = adp.UpdateAB4(Convert.ToDecimal(this.txtAB4.Text.Trim()), this.txtUserAccountAB4.Text.Trim());

            if (flag < 1)
            {
                msg = "设置失败!";
            }

        }
        catch (Exception ex)
        {
            msg = ex.Message;
        }


        Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('" + msg + "');", true);


        this.tab_SetAB4.Visible = false;
    }
    /// <summary>
    /// 重置密钥
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbtnSetMiYao_Command(object sender, CommandEventArgs e)
    {

        string msg = "操作成功!\\n密钥已设置为:666666";
        try
        {
            string[] userInfo = e.CommandArgument.ToString().Split('|');
            SQLExpressDAL.DealRecord obj = new SQLExpressDAL.DealRecord();
            int flag = obj.UpdateUserMiYao(userInfo[0], userInfo[1], "666666");

            if (flag == 0)
            {
                msg = "密钥更改失败!";
            }
        }
        catch
        {
            msg = "密钥设置失败!";
        }

        ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('" + msg + "')", true);
    }

}
